package com.wpre.transaction.controller;

import com.wpre.transaction.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-11-29
 * Time: 13:57
 */
@Slf4j
@RestController
@RequestMapping("trans")
public class TransactionalController {
    @Autowired
    UserService userService;

    /**
     * 不加事务的情况下, 数据插入成功
     *
     * @param name
     * @param password
     * @return
     */
//    @Transactional
    @RequestMapping("/r1")
    public String registry(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        int a = 10 / 0;
        return "注册成功";
    }

    /**
     * 添加事务的情况,发生异常: 事务回滚
     */
    @Transactional
    @RequestMapping("/r2")
    public String r2(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        int a = 10 / 0;
        return "注册成功";
    }


    /**
     * 异常被捕获: 事务提交
     */
    @Transactional
    @RequestMapping("/r3")
    public String r3(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("发生异常");
        }

        return "注册成功";
    }

    /**
     * 异常被捕获,但是throw出去了: 事务回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public String r4(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("发生异常");
            throw e;
        }

        return "注册成功";
    }

    /**
     * 手动回滚事务
     */
    @Transactional
    @RequestMapping("/r5")
    public String r5(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("发生异常");
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return "注册成功";
    }


    /**
     * 发生非运行时异常, 事务提交
     */
    @Transactional
    @RequestMapping("/r6")
    public String r6(String name, String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        if (true) {
            throw new IOException();
        }

        return "注册成功";

    }

    /**
     * 设置rollbackFor, 发生非运行时异常, 事务也会回滚
     * rollbackForClassName表示让程序发生某个异常时,也进行回滚
     */
//    @Transactional(rollbackFor = Throwable.class)
//    @Transactional(rollbackForClassName = "Exception")
    @Transactional(rollbackFor = NullPointerException.class)
    @RequestMapping("/r7")
    public String r7(String name, String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        if (true) {
            throw new IOException();
        }

        return "注册成功";
    }


    /**
     * 设置事务的隔离级别
     */
    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r8")
    public String r8(String name, String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功, 影响行数: " + result);
        }
        if (true) {
            throw new IOException();
        }

        return "注册成功";
    }
}
